package cn.zxf.note.note.parse;

import cn.zxf.note.BaseAppTest4;
import cn.zxf.note.common.constant.UserConstant;
import cn.zxf.note.note.note.NoteSupport;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

/**
 * Created by zengxf on 2019/7/23.
 */
@Slf4j
public class SaveAndParseMonthNote extends BaseAppTest4 {

    @Autowired
    NoteSupport support;

    @Test
    public void parseFolderAndSave() throws IOException {
        String folder = "C:\\Users\\Administrator\\Desktop\\aa\\month-note";
        Files.find(Paths.get(folder), 1, (path, attr) -> {
            String fileName = path.getFileName().toString().toLowerCase();
            log.info("file: {}", fileName);
            return attr.isRegularFile() && !fileName.startsWith("test") && fileName.endsWith(".md");
        }).forEach(path -> {
            log.info("path: {}", path);
            try {
                this.parseAndSave(path);
            } catch (IOException e) {
                log.error("解析出错！", e);
            }
        });
    }

    private void parseAndSave(Path path) throws IOException {
        MonthNoteVo vo = ParseMonthNote.of(path.toString()).parse();
        log.info("month: {}", vo.getMonth());
        log.info("--------------");
        log.info("--------------");
        vo.getDayNotes().forEach(day -> {
            log.info("day: {}", day.getDay());
            day.getNotes().stream()
                    .collect(Collectors.groupingBy(NoteVo::getType, LinkedHashMap::new, Collectors.toList()))
                    .forEach((type, notes) ->
                            notes.forEach(note ->
                                    support.saveForImport(UserConstant.USER8, vo.getMonth(), day.getDay(), note)
                            )
                    );
        });
    }


}